﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using FBProject.BusinessLogic;

namespace FBProject
{
    public partial class FWizard : Form
    {
        public FWizard()
        {
            InitializeComponent();

            this.Text = ApplicationHelper.GetApplicationNameAndVersion(Assembly.GetExecutingAssembly().GetName());

            SynchronizeButton.Visible = false;
            SynchronizeButton.Location = NextButton.Location;
        }

        private void FWizard_Load(object sender, EventArgs e)
        {
            EnableButtons(true);
        }

        private void ActiveProjectInfoControl_ActiveProjectChanged(object sender, EventArgs e)
        {
            EnableButtons(true);
        }

        private void FBExportFileSelectorControl_FBExportFileChanged(object sender, EventArgs e)
        {
            EnableButtons(false);
        }

        private void CancelWizardButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you really want to exit FBProject?", "QUESTION", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void HowToPrepareYourMSProjectLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://fbproject.codeplex.com/wikipage?title=How%20to%20prepare%20your%20MS%20Project");
        }

        private void HowToExportCasesFromFogBugzLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://fbproject.codeplex.com/wikipage?title=How%20to%20export%20cases%20from%20FogBugz");
        }

        private void HowSynchronizationWorksLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://fbproject.codeplex.com/wikipage?title=How%20synchronization%20works");
        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            if (WizardControl.SelectedTab.Name == ProjectTabPage.Name)
            {
                WizardControl.SelectTab(FBExportFileTabPage);
            }
            else if (WizardControl.SelectedTab.Name == FBExportFileTabPage.Name)
            {
                WizardControl.SelectTab(SummaryTabPage);
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            if (WizardControl.SelectedTab.Name == FBExportFileTabPage.Name)
            {
                WizardControl.SelectTab(ProjectTabPage);
            }
            else if (WizardControl.SelectedTab.Name == SummaryTabPage.Name)
            {
                WizardControl.SelectTab(FBExportFileTabPage);
            }
        }

        private void WizardControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableButtons(true);
        }

        private void SynchronizeButton_Click(object sender, EventArgs e)
        {
            Enabled = false;

            DialogResult synchronizationResult;
            IList<TaskDifference> taskDifferences;
            using (var progressForm = new FSynchronizationProgress(FBExportFileSelectorControl.FBExportFileName))
            {
                synchronizationResult = progressForm.ShowDialog(this);
                taskDifferences = progressForm.TaskDifferences;
            }

            if (synchronizationResult == System.Windows.Forms.DialogResult.OK)
            {
                Visible = false;
                using (var taskDifferencesForm = new FTaskDifferences(taskDifferences))
                {
                    taskDifferencesForm.ShowDialog(this);
                }
                Visible = true;
            }

            Enabled = true;
        }

        private void EnableButtons(bool changeFocus)
        {
            NextButton.Visible = (WizardControl.SelectedTab.Name != SummaryTabPage.Name);
            SynchronizeButton.Visible = (WizardControl.SelectedTab.Name == SummaryTabPage.Name);
            BackButton.Enabled = (WizardControl.SelectedTab.Name != ProjectTabPage.Name);

            if (WizardControl.SelectedTab.Name == ProjectTabPage.Name)
            {
                NextButton.Enabled = ActiveProjectInfoControl.ActiveProjectOk;
            }
            else if (WizardControl.SelectedTab.Name == FBExportFileTabPage.Name)
            {
                NextButton.Enabled = FBExportFileSelectorControl.FBExportFileOk;
            }
            else if (WizardControl.SelectedTab.Name == SummaryTabPage.Name)
            {
                SynchronizeButton.Enabled = ActiveProjectInfoControl.ActiveProjectOk && FBExportFileSelectorControl.FBExportFileOk;
            }

            if (changeFocus)
            {
                if (NextButton.Visible && NextButton.Enabled)
                {
                    NextButton.Focus();
                }

                if (SynchronizeButton.Visible && SynchronizeButton.Enabled)
                {
                    SynchronizeButton.Focus();
                }
            }
        }
    }
}
